package org.han.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.han.entity.User;
import org.han.service.ILoginBiz;
import org.han.vo.LoginVo;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	public LoginAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	private ILoginBiz biz;
	private LoginVo lv;

	public LoginVo getLv() {
		return lv;
	}

	public void setLv(LoginVo lv) {
		this.lv = lv;
	}

	public void setBiz(ILoginBiz biz) {
		this.biz = biz;
	}

	@Action(value = "/login", results = {
			@Result(name = "success", location = "/success.jsp"),
			@Result(name = "input", location = "/index.jsp") }, className = "userAction")
	public String login() throws Exception {
		// TODO Auto-generated method stub
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpServletRequest request=ServletActionContext.getRequest();
		String code = request.getSession()
				.getAttribute("rand").toString();
		if (!code.equals(lv.getAuthcode())) {
			out.println(1);
			out.close();
			return null;
		}
		User u = biz.login(lv);
		if (u != null) {
			request.getSession().setAttribute("user", u);
			return SUCCESS;
		} else {
			out.println(2);
			out.close();
			return null;
		}
	}
}
